﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.ComponentModel.Design.Serialization;
using ProgNetExtensions.ExtenderProviders.Serialization;

namespace ProgNetExtensions.DataProviderTools
{
    [DesignerSerializer(typeof(DataSourceProviderSerializer), typeof(CodeDomSerializer))]
    public partial class DataProviderContainer : Component
    {
        private DataProviderCollection _collection;
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public DataProviderCollection Providers
        {
            get
            {
                return _collection;
            }
        }

        public DataProviderContainer()
        {
            _collection = new DataProviderCollection();
            InitializeComponent();
        }
    }

    public class DataProviderCollection : CollectionBase
    {
        public void Add(DataProvider p)
        {
            List.Add(p);
        }
        public void Remove(DataProvider p)
        {
            List.Remove(p);
        }

        public DataProvider this[int i]
        {
            get { return (DataProvider)(List[i]); }
        }

        public DataProvider this[string name]
        {
            get
            {
                foreach (object o in List)
                    if ((o is DataProvider) && (o as DataProvider).Name == name)
                        return o as DataProvider;
                return null;
            }
        }

    }
    internal class DataProviderPropertyDescriptor : PropertyDescriptor
    {
        private DataProviderCollection collection = null;
        private int index = -1;

        public DataProviderPropertyDescriptor(DataProviderCollection coll, int idx)
            :
            base("#" + idx.ToString(), null)
        {
            this.collection = coll;
            this.index = idx;
        }

        public override AttributeCollection Attributes
        {
            get
            {
                return new AttributeCollection(null);
            }
        }

        public override bool CanResetValue(object component)
        {
            return true;
        }

        public override Type ComponentType
        {
            get
            {
                return this.collection.GetType();
            }
        }

        public override string DisplayName
        {
            get
            {
                DataProvider data = this.collection[index];
                return data.Name;
            }
        }

        public override string Description
        {
            get
            {
                DataProvider data = this.collection[index];
                StringBuilder sb = new StringBuilder();
                sb.Append(data.Name);
                if (data.Name != null)
                {
                    sb.Append("[");
                    sb.Append(data.Name);
                    sb.Append("]");
                }
                return sb.ToString();
            }
        }

        public override object GetValue(object component)
        {
            return this.collection[index];
        }

        public override bool IsReadOnly
        {
            get { return false; }
        }

        public override string Name
        {
            get { return "#" + index.ToString(); }
        }

        public override Type PropertyType
        {
            get { return this.collection[index].GetType(); }
        }

        public override void ResetValue(object component)
        {
        }

        public override bool ShouldSerializeValue(object component)
        {
            return true;
        }

        public override void SetValue(object component, object value)
        {
            // this.collection[index] = value;
        }
    }
}
